//
//  JsLogManager.m
//	JsUtilitySdk
//  Created by Gikki Ares on 2021/4/30.
//

#import "JsLogManager.h"
#import "JsFileManager.h"
#import "JsLogBasicData.h"

@interface JsLogManager(){
	BOOL mb_isEnble;
}

@property(nonatomic,strong) JsLogger * logger;

@end

@implementation JsLogManager

	//单例使用默认的.
+ (instancetype)sharedInstance {
	static JsLogManager * _sharedInstance;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		_sharedInstance = [[JsLogManager alloc]init];
		_sharedInstance.logger = [JsLogger sharedInstance];
	});
	return _sharedInstance;
}


- (instancetype)initWithLogDirPath:(NSString *)logDirPath {
	if(self = [super init]) {
		self.logger = [[JsLogger alloc]initWithLogDirPath:logDirPath initialContent:nil];
	}
	return self;
}

- (instancetype)initWithLogDirPath:(NSString *)logDirPath fileInitalContent:(NSString *)initialContent {
	if(self = [super init]) {
		self.logger = [[JsLogger alloc]initWithLogDirPath:logDirPath initialContent:initialContent];
	}
	return self;
}


-(void)logWithLevel:(JsLogLevel)level target:(JsLogTargetOption)targetOption context:(NSString * __nullable)context contentType:(NSString * __nullable)contentType content:(NSString *)conent {
	JsLogEvent * logEvent = [JsLogEvent eventWithLogLevel:level logTarget:targetOption context:context contentType:contentType message:conent];
	logEvent.plain = self.plain;
	[self.logger log:logEvent];
}

/**
 
 $1,
 $2,
 $3,
 $4,cotnentType,日志内容的类别,便于按照类别查看日志.
 $5,format,
 */
- (void)logWithLevel:(JsLogLevel)level target:(JsLogTargetOption)targetOption context:(NSString *)context contentType:(NSString *)contentType format:(NSString *)format, ... {
	va_list args;
	va_start(args, format);
	NSString *content = [[NSString alloc] initWithFormat:format arguments:args];
	va_end(args);
	[self logWithLevel:level target:targetOption context:context contentType:contentType content:content];
}

	//设置日志的等级和标签
-(void)logWithLevel:(JsLogLevel)level target:(JsLogTargetOption)targetOption context:(NSString *)context format:(NSString *)format, ...{
	va_list args;
	if (format) {
		va_start(args, format);
		NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
		va_end(args);
		[self logWithLevel:level target:targetOption context:context contentType:nil  content:message];
	}
}

	//等级值越小越严重,大于该值的,不输出.
- (void)setLogLevelFilter:(int)filter {
	self.logger.logLevel = filter;
}
	//设置了标签后,只有标签中的日志才会输出,其余不输出.
- (void)setLogTags:(NSArray *)logTags {
	self.logger.logTags = logTags;
}

	//设置日志输出目录
- (void)setLogDirPath:(NSString *)logDirPath {
	self.logger.fileTarget.logDirectoryPath = logDirPath;
}
	//获取日志目录路径
- (NSString *)getLogDirPath {
	return self.logger.fileTarget.logDirectoryPath;
}
- (void)printLogFileDir {
	NSString * logDirPath = [self getLogDirPath];
	jslogv_fc(@"JsLogManager",nil, @"Log dir is:%@",logDirPath);
}

	//获取今日的日志文件路径
- (NSString *)getTodayLogFilePath {
	return self.logger.fileTarget.currentFilePath;
}

	//设置最大的日志文件数量
- (void)setMaxLogFileCount:(int)count {
	self.logger.fileTarget.maxFileCount = count;
}

- (void)clearLogDir {
	[JsFileManager emptyDir:[self getLogDirPath]];
}
- (void)clearTodayLog {
	NSString * filePath = [self getTodayLogFilePath];
	[JsFileManager emptyFile:filePath];
}
@end
